package com.roc.admin.interfaces.controller;

import com.roc.admin.application.service.MenuService;
import com.roc.admin.interfaces.common.Result;
import com.roc.admin.interfaces.dto.MenuDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单控制器
 */
@Tag(name = "菜单管理", description = "菜单管理相关接口")
@RestController
@RequestMapping("/menu")
public class MenuController {

    private final MenuService menuService;

    public MenuController(MenuService menuService) {
        this.menuService = menuService;
    }

    @Operation(summary = "创建菜单")
    @PostMapping
    public Result<Long> createMenu(@Validated @RequestBody MenuDTO menuDTO) {
        Long menuId = menuService.createMenu(menuDTO);
        return Result.success(menuId);
    }

    @Operation(summary = "更新菜单")
    @PutMapping
    public Result<Boolean> updateMenu(@Validated @RequestBody MenuDTO menuDTO) {
        boolean success = menuService.updateMenu(menuDTO);
        return Result.success(success);
    }

    @Operation(summary = "删除菜单")
    @DeleteMapping("/{menuId}")
    public Result<Boolean> deleteMenu(@Parameter(description = "菜单ID") @PathVariable Long menuId) {
        boolean success = menuService.deleteMenu(menuId);
        return Result.success(success);
    }

    @Operation(summary = "获取菜单信息")
    @GetMapping("/{menuId}")
    public Result<MenuDTO> getMenuById(@Parameter(description = "菜单ID") @PathVariable Long menuId) {
        MenuDTO menuDTO = menuService.getMenuById(menuId);
        return Result.success(menuDTO);
    }

    @Operation(summary = "获取菜单列表")
    @GetMapping("/list")
    public Result<List<MenuDTO>> getMenuList() {
        List<MenuDTO> menuDTOList = menuService.getMenuList();
        return Result.success(menuDTOList);
    }

    @Operation(summary = "获取菜单树")
    @GetMapping("/tree")
    public Result<List<MenuDTO>> getMenuTree() {
        List<MenuDTO> menuTree = menuService.getMenuTree();
        return Result.success(menuTree);
    }

    @Operation(summary = "根据用户ID获取菜单树")
    @GetMapping("/tree/user/{userId}")
    public Result<List<MenuDTO>> getMenuTreeByUserId(@Parameter(description = "用户ID") @PathVariable Long userId) {
        List<MenuDTO> menuTree = menuService.getMenuTreeByUserId(userId);
        return Result.success(menuTree);
    }

    @Operation(summary = "根据角色ID获取菜单列表")
    @GetMapping("/list/role/{roleId}")
    public Result<List<MenuDTO>> getMenuListByRoleId(@Parameter(description = "角色ID") @PathVariable Long roleId) {
        List<MenuDTO> menuDTOList = menuService.getMenuListByRoleId(roleId);
        return Result.success(menuDTOList);
    }
}
